package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.AnnouncementMapper;
import com.zcoj.pojo.Dto.Announcement;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.AnnouncementService;
import com.zcoj.util.RedisCaches;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * Service实现
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;


    @Autowired
    private RedisCaches redisCaches;

    @Override
    public Integer create(Announcement announcement) {
        LoginUser loginUser = redisCaches.getLoginUser();
        announcement.setUid(loginUser.getId());
        announcement.setAuthor(loginUser.getUsername());
        announcementMapper.insert(announcement);
        return announcement.getId();
    }

    @Override
    public Integer deleteById(Integer id) {
        return announcementMapper.deleteById(id);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        announcementMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer updateById(Announcement announcement) {
       return announcementMapper.updateById(announcement);
    }

    @Override
    public void updateByIdInBatch(List<Announcement> announcements) {
        if (CollectionUtils.isEmpty(announcements)) return;
        for (Announcement announcement: announcements) {
            this.updateById(announcement);
        }
    }

    @Override
    public Announcement findById(Integer id) {
        return announcementMapper.selectById(id);
    }

    @Override
    public Announcement findOne(Announcement announcement) {
        Wrapper<Announcement> wrapper = new QueryWrapper<>(announcement);
        return announcementMapper.selectOne(wrapper);
    }

    @Override
    public List<Announcement> findList(Announcement announcement) {
        Wrapper<Announcement> wrapper = new QueryWrapper<>(announcement);
        return announcementMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<Announcement> findPage(PageWrap<Announcement> pageWrap) {
        IPage<Announcement> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        Announcement announcement = WrapperUtil.blankToNull(pageWrap.getModel());
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        if( Objects.nonNull(announcement.getTitle())){
            queryWrapper.like("title",announcement.getTitle());
        }
        if(Objects.nonNull(announcement.getStatus())){
            queryWrapper.eq("status",announcement.getStatus());
        }
        queryWrapper.select("id","title","status", "cid","gid","author","create_time","update_time");
        return PageData.from(announcementMapper.selectPage(page, queryWrapper));
    }


    @Override
    public PageData<Announcement> findPageVo(PageWrap<Announcement> pageWrap) {
        LoginUser loginUser = redisCaches.getLoginUser();

        IPage<Announcement> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        Announcement announcement = WrapperUtil.blankToNull(pageWrap.getModel());
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        if(Objects.isNull(loginUser)){
            queryWrapper.eq("status",1);
        }else {
            if(Objects.nonNull(announcement.getAuthor())){
                if(!(loginUser.getPermission().contains("admin")||loginUser.getPermission().contains("super_admin"))){
                    queryWrapper.eq("status",1);
                }
            }
        }


       queryWrapper.eq("cid",0).eq("gid",0);
        return PageData.from(announcementMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(Announcement announcement) {
        Wrapper<Announcement> wrapper = new QueryWrapper<>(announcement);
        return announcementMapper.selectCount(wrapper);
    }
}
